package cat.sun.dialogs;

import java.util.List;

import cat.sun.R;
import cat.sun.stats.Stats;
import cat.sun.stats.StatsValues;
import cat.sun.stats.StringScore;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

public class LeaderboardsDialog extends DialogFragment
{
	public static LeaderboardsDialog newInstance()
	{
		LeaderboardsDialog dialog = new LeaderboardsDialog();		
		return dialog;
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) 
	{		
		super.onCreate(savedInstanceState);
		setStyle(DialogFragment.STYLE_NO_FRAME, 0);	
		//setCancelable(false);
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) 
	{
		View view = inflater.inflate(R.layout.dialog_leaderboards, container,false);		
		final Context context = getActivity();		
		
		final List<StringScore> items1 = new Stats(context.getSharedPreferences(StatsValues.EASY_KEY, 0),StatsValues.EASY_KEY,getResources()).getStringScores();
		final List<StringScore> items3 = new Stats(context.getSharedPreferences(StatsValues.MEDIUM_KEY, 0),StatsValues.MEDIUM_KEY,getResources()).getStringScores();
		final List<StringScore> items5 = new Stats(context.getSharedPreferences(StatsValues.INSANE_KEY, 0),StatsValues.INSANE_KEY,getResources()).getStringScores();
		
		ScrollView root =  (ScrollView) view.findViewById(R.id.dialog_leader_scroll);	 
		LinearLayout layout = new LinearLayout(context);
		layout.setOrientation(LinearLayout.VERTICAL);
		
		final ViewHolder [] holders = new ViewHolder[10];
				
		for (int i = 0;i<items1.size();i++)
		{
			FrameLayout item = (FrameLayout) getActivity().getLayoutInflater().inflate(R.layout.item_dialog_leaderboards, null);
			TextView name = (TextView) item.findViewById(R.id.dialog_leader_name);
			TextView number = (TextView) item.findViewById(R.id.dialog_leader_number);
			TextView time = (TextView) item.findViewById(R.id.dialog_leader_time);
			
			number.setTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Medium.ttf"));
			name.setTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Black.ttf"));
			time.setTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Medium.ttf"));
			
			switch (i) {
			
			
			case 9: 
				number.setTextColor(Color.argb(255, 72, 96, 105));
				name.setTextColor(Color.argb(255, 72, 96, 105));
				time.setTextColor(Color.argb(255, 72, 96, 105));
				break;
				
			case 8: 
				number.setTextColor(Color.argb(255, 70, 106, 119));
				name.setTextColor(Color.argb(255, 70, 106, 119));
				time.setTextColor(Color.argb(255, 70, 106, 119));
				break;	
				
			case 7: 
				number.setTextColor(Color.argb(255, 67, 115, 132));
				name.setTextColor(Color.argb(255, 67, 115, 132));
				time.setTextColor(Color.argb(255, 67, 115, 132));
				break;
			
			case 6: 
				number.setTextColor(Color.argb(255, 65, 124, 146));
				name.setTextColor(Color.argb(255, 65, 124, 146));
				time.setTextColor(Color.argb(255, 65, 124, 146));
				break;
				
			case 5: 
				number.setTextColor(Color.argb(255, 63, 134, 159));
				name.setTextColor(Color.argb(255, 63, 134, 159));
				time.setTextColor(Color.argb(255, 63, 134, 159));
				break;
				
			case 4: 
				number.setTextColor(Color.argb(255, 60, 143, 173));
				name.setTextColor(Color.argb(255, 60, 143, 173));
				time.setTextColor(Color.argb(255, 60, 143, 173));
				break;
				
			case 3: 
				number.setTextColor(Color.argb(255, 58, 152, 186));
				name.setTextColor(Color.argb(255, 58, 152, 186));
				time.setTextColor(Color.argb(255, 58, 152, 186));
				break;	
				
			case 2: 
				number.setTextColor(Color.argb(255, 56, 161, 200));
				name.setTextColor(Color.argb(255, 56, 161, 200));
				time.setTextColor(Color.argb(255, 56, 161, 200));
				break;
				
			case 1: 
				number.setTextColor(Color.argb(255, 53, 171, 227));
				name.setTextColor(Color.argb(255, 53, 171, 227));
				time.setTextColor(Color.argb(255, 53, 171, 227));
				break;
				
			case 0: 
				number.setTextColor(Color.argb(255, 51, 180, 227));
				name.setTextColor(Color.argb(255, 51, 180, 227));
				time.setTextColor(Color.argb(255, 51, 180, 227));
				break;
					
			}
			
			if (i!=9) number.setText(Integer.toString(i+1)+".  ");
			else number.setText(Integer.toString(i+1)+".");
			
			ViewHolder holder = new ViewHolder();
			holder.name = name;
			holder.number = number;
			holder.time = time;
			holder.position = i;
			holders[i] = holder;
			
			layout.addView(item);
		}
		root.addView(layout);

		TextView title = (TextView) view.findViewById(R.id.dialog_leader_title);
		title.setTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Black.ttf"));
		
		final TextView tab1 = (TextView) view.findViewById(R.id.dialog_leader_tab1);		
		final TextView tab3 = (TextView) view.findViewById(R.id.dialog_leader_tab3);		
		final TextView tab5 = (TextView) view.findViewById(R.id.dialog_leader_tab5);
		
		tab1.setTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Black.ttf"));
		tab3.setTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Black.ttf"));
		tab5.setTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Black.ttf"));
		
		final HorizontalScrollView scrollView = (HorizontalScrollView) view.findViewById(R.id.dialog_leader_scroll_hor);
		
		for (int i = 0;i<items1.size();i++)
		{
			holders[i].name.setText(items1.get(i).getName());
			if (isFloat(items1.get(i).getScore()))
			holders[i].time.setText(String.format("%.2f",Float.parseFloat(items1.get(i).getScore())));			
			else holders[i].time.setText(items1.get(i).getScore());
		}
		tab1.setBackgroundResource(R.drawable.saper_tab_pressed);
				
		tab1.setOnClickListener(new View.OnClickListener() 
		{			
			@Override
			public void onClick(View v) 
			{
				for (int i = 0;i<items1.size();i++)
				{
					holders[i].name.setText(items1.get(i).getName());
					if (isFloat(items1.get(i).getScore()))
						holders[i].time.setText(String.format("%.2f",Float.parseFloat(items1.get(i).getScore())));			
						else holders[i].time.setText(items1.get(i).getScore());
				}				
				tab1.setBackgroundResource(R.drawable.saper_tab_pressed);
				tab3.setBackgroundResource(R.drawable.saper_tab);				
				tab5.setBackgroundResource(R.drawable.saper_tab);
				scrollView.scrollTo(0, 0);
			}
		});
		
		tab3.setOnClickListener(new View.OnClickListener() 
		{			
			@Override
			public void onClick(View v) 
			{
				for (int i = 0;i<items3.size();i++)
				{
					holders[i].name.setText(items3.get(i).getName());
					if (isFloat(items3.get(i).getScore()))
						holders[i].time.setText(String.format("%.2f",Float.parseFloat(items3.get(i).getScore())));			
						else holders[i].time.setText(items3.get(i).getScore());
					}
				
				tab1.setBackgroundResource(R.drawable.saper_tab);
				tab3.setBackgroundResource(R.drawable.saper_tab_pressed);
				tab5.setBackgroundResource(R.drawable.saper_tab);
			}
		});
		
		tab5.setOnClickListener(new View.OnClickListener() 
		{			
			@Override
			public void onClick(View v)
			{				
				for (int i = 0;i<items5.size();i++)
				{
					holders[i].name.setText(items5.get(i).getName());
					if (isFloat(items5.get(i).getScore()))
						holders[i].time.setText(String.format("%.2f",Float.parseFloat(items5.get(i).getScore())));			
						else holders[i].time.setText(items5.get(i).getScore());
					}				
				tab1.setBackgroundResource(R.drawable.saper_tab);
				tab3.setBackgroundResource(R.drawable.saper_tab);				
				tab5.setBackgroundResource(R.drawable.saper_tab_pressed);
				scrollView.scrollTo(scrollView.getRight(), 0);
			}
		});
				
		final ImageView close = (ImageView) view.findViewById(R.id.dialog_leader_close);
		
		close.setOnClickListener(new View.OnClickListener() 
		{			
			@Override
			public void onClick(View v) 
			{
				dismiss();
			}			
		});
		
		return view;
	}
	
	static class ViewHolder 
	{
		  TextView time;
		  TextView name;
		  TextView number;
		  int position;
	}	
	
	private boolean isFloat(String f)
	{
		try
		{
			Float.parseFloat(f);
			return true;
		}
		catch (NumberFormatException e)
		{
			return false;
		}
	}
}
